﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

public class SpawnTrigger : TriggerObject
{
    private Random rand = new Random();
    private int randLimit = 1;
    private double dropProbability = .01;
    protected new SpawnTrigger.Type thisType;
    protected DestructableObject.Type dropType = DestructableObject.Type.Boulder;

    /**
     * creates a CollidableObject at 'placement' of type 'type'.
     * initializes origin, and
     * type. This will set the SpawnPointer for this instance
     * to null.
     * @param type an instance of ObjectFactory.TypeEnum which
     * specifies what type of CollidableObject to create
     * @param placement the world x,y,z coordinates that define
     * where this instance is placed.
     * @see ObjectFactory.TypeEnum
     */
    public SpawnTrigger(SpawnTrigger.Type type, Vector3 placement)
        : base(type, placement)
    {
        thisType = type;
    }
    
    public override void update(double elapsed)
    {
        //check to see if the player is in the trigger, if so, spawn falling objects of the specified type
        if (rand.NextDouble() <= dropProbability)
        {
            base.update(elapsed);
            List<CollidableObject> colliders = LevelBase.collidesWith(this);
            foreach (CollidableObject co in colliders)
            {
                if (co is QuinHero)
                {
                    dropItem(co.position);
                    break;
                }
            }
        }
    }

    private void dropItem(Vector3 target)
    {
        Vector3 initPoint = new Vector3(
                    target.X + rand.Next(randLimit * 2) - randLimit,
                    target.Y - 1000,
                    target.Z -(dropType.collisionBoxDimentions.Z/2)+ rand.Next(randLimit * 2) - randLimit);
        Vector3 dropMin = initPoint-dropType.collisionBoxOriginOffset;
        Vector3 dropMax = dropMin+dropType.collisionBoxDimentions;
        Vector3 diff;
        /*
        if (dropMin.X < min.X)
        {
            diff = new Vector3(min.X - dropMin.X,0,0);
            initPoint += diff;
            dropMin += diff;
            dropMax += diff;
        }*/
        if (dropMin.Z < min.Z)
        {
            diff = new Vector3(0,0,min.Z - dropMin.Z);
            initPoint += diff;
            dropMin += diff;
            dropMax += diff;
        }
        /*
        if (dropMax.X > max.X)
        {
            diff = new Vector3(max.X - dropMax.X, 0, 0);
            initPoint += diff;
            dropMin += diff;
            dropMax += diff;
        }*/
        if (dropMax.Z > max.Z)
        {
            diff = new Vector3(0, 0, max.Z - dropMax.Z);
            initPoint += diff;
            dropMin += diff;
            dropMax += diff;
        }
        DestructableObject drop = new DestructableObject(dropType,initPoint);
        drop.stayInMemory(true);
        drop.setVelocity(drop.maxDnSpeed);
        LevelBase.addObject(drop);
    }

    public new class Type:TriggerObject.Type
    {
        //triggers
        public static readonly Type BoulderTrigger = new Type(
            ObjectFactory.TypeEnum.BoulderTrigger,//type
            new Vector3(0,200,0), //origin offset
            new Vector3(500,200,996));//collision cube size

        //actual class data

        /**
         * this is a dummy constructor.
         */
        protected Type() { }

        /**
         * Private constructor.
         * This private constructor ensures that only 
         * the pre-specified Type objects can be used 
         * and compared against.
         * @param origin
         * @param collision
         * @param basic
         * @param destruction
         */
        protected Type(ObjectFactory.TypeEnum type, Vector3 origin, Vector3 collision)
            : base(type, origin, collision)
        {
        }

        public static SpawnTrigger.Type getType(ObjectFactory.TypeEnum type)
        {
            switch (type)
            {
                case ObjectFactory.TypeEnum.BoulderTrigger: return BoulderTrigger;
            }
            return null;
        }
    }
}
